package com.gmcc.docmanager.util;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class PackageUtil {

	private static final String TAG = "PackageUtil";

	public static void installPackage(Context context, String filePath)
			throws FileNotFoundException, Exception {
		File file = new File(filePath);
		if (!file.exists()) {
			throw new FileNotFoundException("File is not exists filePath = "
					+ filePath);
		}
		Intent it = new Intent(Intent.ACTION_VIEW);
		it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		it.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		context.startActivity(it);
	}

	public static boolean isExternalStorageMounted() {
		String state = Environment.getExternalStorageState();
		Log.d(TAG, "getExternalStorageState = " + state);
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}

	public static long getExternalStoragetAvailableSize() {
		File path = Environment.getExternalStorageDirectory();
		StatFs statfs = new StatFs(path.getPath());
		int blockSize = statfs.getBlockSize();
		int availableBlocks = statfs.getAvailableBlocks();
		long freeSize = (long) blockSize * (long) availableBlocks;
		Log.d(TAG, "sdcard available size: " + freeSize);
		return freeSize;
	}
}
